<?php

include_once('admin.php');

/**
 * The Reports Management Class provides functions for generating reports and mail-merge files
 * @package admin
 */
class reports_management extends admin
{
	function __construct()
    {
        // Call the Model constructor
        parent::__construct();

		// Load required helpers
		$this->load->helper('download');
		
		// Load the required models
		$this->load->model('reports_model');
		$this->load->model('location_model');
    }
	
	/**
     * Get all volumes within a specified call number range (parameters set in $_POST - see code for more details)
     */
	public function callNumberRange()
	{
		// If 'POST' data exists (i.e. the submit button was clicked)
		if ( $this->input->post() )
		{
			// Get volumes by branch
			$this->data['volumes'] = $this->reports_model->getCallNumberRange( $this->input->post('location_id'), $this->input->post('prefix'), $this->input->post('low_call_number'), $this->input->post('high_call_number') );
			
			$this->load->view('admin/reports/call_number_range_report', $this->data);
		}
		
		else
		{
			// Get list of locations
			$this->data['locations'] = $this->location_model->getAllLocations( )->result();
		
			// Load the "view_volume" view
			$this->data['main_content'] = $this->load->view('admin/reports/call_number_range_form', $this->data, true);
		
			// Load $this->data['main_content'] in the main template
			$this->load->view($this->template, $this->data);
		}		
	}
	
	/**
     * Get all volumes for a specified branch (parameters set in $_POST - see code for more details)
     */
	public function volumesByBranch()
	{

		// If 'POST' data exists (i.e. the submit button was clicked)
		if ( $this->input->post() )
		{
			// Get volumes by branch
			$this->data['volumes'] = $this->reports_model->getVolumesByBranch( $this->input->post('location_id') );
			
			$this->load->view('admin/reports/volumes_by_branch_report', $this->data);
		}
		
		else
		{
			// Get list of locations
			$this->data['locations'] = $this->location_model->getAllLocations( )->result();
		
			// Load the "view_volume" view
			$this->data['main_content'] = $this->load->view('admin/reports/volumes_by_branch_form', $this->data, true);
		
			// Load $this->data['main_content'] in the main template
			$this->load->view($this->template, $this->data);
		}
	}
	
	/**
     * Get all overdue loans (parameters set in $_POST - see code for more details)
     */
	public function overdueLoans()
	{
		if ( $this->input->post() )
		{
			// Load the "catalogue reports" view
			if ( $this->input->post('generate_report') )
			{
				$this->data['loans'] = $this->reports_model->getOverdueLoans( $this->input->post('location_id'), $this->input->post('due_date') );
				$this->load->view('admin/reports/overdue_loans_report', $this->data);
			}
			
			// Load the "catalogue reports" view
			if ( $this->input->post('generate_mail_merge') )
			{
				$this->data['loans'] = $this->reports_model->getOverdueLoans( $this->input->post('location_id'), $this->input->post('due_date'), 'tbl_subscriber.subscriber_id' );
				$this->load->view('admin/reports/overdue_loans_mail_merge', $this->data);
			}
		}
		
		else
		{
			// Get list of locations
			$this->data['locations'] = $this->location_model->getAllLocations( )->result();
		
			// Load the "view_volume" view
			$this->data['main_content'] = $this->load->view('admin/reports/overdue_loans_form', $this->data, true);
		
			// Load $this->data['main_content'] in the main template
			$this->load->view($this->template, $this->data);
		}
	}
	
	/**
     * Get all subscriber in arrears (parameters set in $_POST - see code for more details)
     */
	public function subscribersInArrears()
	{
		if ( $this->input->post() )
		{
			// Get overdue Subscribers in arrears
			$this->data['subscribers'] = $this->reports_model->getSubscribersInArrears( $this->input->post('today') );
	
			// Load the "catalogue reports" view
			if ( $this->input->post('generate_report') )
				$this->load->view('admin/reports/subscribers_in_arrears_report', $this->data);
				
			// Load the "catalogue reports" view
			if ( $this->input->post('generate_mail_merge') )
				$this->load->view('admin/reports/subscribers_in_arrears_mail_merge', $this->data);
		}
		
		else
		{
			// Load the "view_volume" view
			$this->data['main_content'] = $this->load->view('admin/reports/subscribers_in_arrears_form', $this->data, true);
		
			// Load $this->data['main_content'] in the main template
			$this->load->view($this->template, $this->data);
		}
	}
	
	/**
     * Get all subscribers in arrears who also have overdue loans (parameters set in $_POST - see code for more details)
     */
	public function subscribersInArrearsWithOverdueLoans()
	{
		if ( $this->input->post() )
		{
			// Get overdue Subscribers in arrears
			$this->data['subscribers'] = $this->reports_model->getSubscribersInArrearsWithOverdueLoans( $this->input->post('today') );
	
			// Load the "catalogue reports" view
			if ( $this->input->post('generate_report') )
				$this->load->view('admin/reports/subscribers_in_arrears_with_overdue_loans_report', $this->data);
				
			// Load the "catalogue reports" view
			if ( $this->input->post('generate_mail_merge') )
				$this->load->view('admin/reports/subscribers_in_arrears_with_overdue_loans_mail_merge', $this->data);
		}
		
		else
		{
			// Load the "view_volume" view
			$this->data['main_content'] = $this->load->view('admin/reports/subscribers_in_arrears_with_overdue_loans_form', $this->data, true);
			
			// Load $this->data['main_content'] in the main template
			$this->load->view($this->template, $this->data);
		}
	}
	
	/**
     * Get all financial subscribers (parameters set in $_POST - see code for more details)
     */
	public function financialSubscribers()
	{
		if ( $this->input->post() )
		{
			// Get overdue Subscribers in arrears
			$this->data['subscribers'] = $this->reports_model->getFinancialSubscribers( $this->input->post('today') );
	
			// Load the "catalogue reports" view
			if ( $this->input->post('generate_report') )
				$this->load->view('admin/reports/financial_subscribers_report', $this->data);
				
			// Load the "catalogue reports" view
			if ( $this->input->post('generate_mail_merge') )
				$this->load->view('admin/reports/subscribers_in_arrears_mail_merge', $this->data);
		}
		
		else
		{
			// Load the "view_volume" view
			$this->data['main_content'] = $this->load->view('admin/reports/financial_subscribers_form', $this->data, true);
			
			// Load $this->data['main_content'] in the main template
			$this->load->view($this->template, $this->data);
		}
	}

	/**
     * Generate mailing labels (parameters set in $_POST - see code for more details)
	 * @todo Write function
     */
	public function mailingLabels()
	{
		if ( $this->input->post() )
		{
			// Get overdue Subscribers in arrears
			$this->data['subscribers'] = $this->reports_model->getActiveAndHonorarySubscribers( );
	
			// Load the "catalogue reports" view
			if ( $this->input->post('generate_report') )
				$this->load->view('admin/reports/mailing_labels_report', $this->data);
				
			// Load the "catalogue reports" view
			if ( $this->input->post('generate_csv') )
				$this->load->view('admin/reports/subscribers_in_arrears_mail_merge', $this->data);
		}
		
		else
		{
			// Get Active and Honorary Subscriber Count
			$this->data['mailing_list_count'] = $this->reports_model->getActiveAndHonorarySubscribers( )->num_rows();
		
			// Load the "view_volume" view
			$this->data['main_content'] = $this->load->view('admin/reports/mailing_labels_form', $this->data, true);
			
			// Load $this->data['main_content'] in the main template
			$this->load->view($this->template, $this->data);
		}		
	}
	
	/**
     * Inactivate subscribers who's subscriptions expired before the date specified in $_POST
     */
	public function inactivateSubscribers( )
	{
		// Redirect if not logged in as admin
		if ( !$this->session->userdata('CurrentUser') || !$this->session->userdata('CurrentUser')->su_access )
			redirect('authenticate/login');
		
		if ( $this->input->post() )
		{
			// Get overdue Subscribers in arrears
			$count = $this->data['subscribers'] = $this->reports_model->inactivateSubscribers( $this->input->post('as_of_date') );
			
			$this->data['error'] = $count . ' subscribers have been set to inactive.';
		}
		
		// Load the "view_volume" view
		$this->data['main_content'] = $this->load->view('admin/reports/inactivate_subscribers_form', $this->data, true);
			
		// Load $this->data['main_content'] in the main template
		$this->load->view($this->template, $this->data);	    
	}
}

?>